package com.hh.service.service.impl.merchant;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.common.model.merchant.Merchant;
import com.hh.common.model.merchant.MerchantAdmin;
import com.hh.common.model.merchant.MerchantRole;
import com.hh.common.page.CommonPage;
import com.hh.common.request.BrokerageRecordRequest;
import com.hh.common.request.PageParamRequest;
import com.hh.common.request.merchant.AddMerchantRequest;
import com.hh.common.response.merchant.MerchantListResponse;
import com.hh.common.utils.HhUtil;
import com.hh.common.vo.merchant.MerAppIdInfoVo;
import com.hh.service.dao.merchant.MerchantDao;
import com.hh.service.service.CategoryService;
import com.hh.service.service.SystemConfigService;
import com.hh.service.service.merchant.MerchantAdminService;
import com.hh.service.service.merchant.MerchantMenuService;
import com.hh.service.service.merchant.MerchantRoleService;
import com.hh.service.service.merchant.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantDao,Merchant> implements MerchantService {

    @Resource
    private MerchantDao dao;

    @Autowired
    private MerchantRoleService merchantRoleService;

    @Autowired
    private MerchantAdminService merchantAdminService;

    @Autowired
    private MerchantMenuService merchantMenuService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SystemConfigService systemConfigService;

    @Override
    public MerAppIdInfoVo getMerAppIdById(String merId) {
        return dao.getMerAppIdById(merId);
    }

    @Override
    public PageInfo<MerchantListResponse> getList(BrokerageRecordRequest request, PageParamRequest pageParamRequest) {
        Page<MerchantListResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<MerchantListResponse> list = dao.getList();
        return CommonPage.copyPageInfo(page, list);
    }

    /**
     * 添加商户
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMerchant(AddMerchantRequest request) {
        Merchant merchant = new Merchant();
        BeanUtil.copyProperties(request,merchant);

        //添加商户基资料
        save(merchant);
        //初始化商户超级管理员角色
        String merId = merchant.getId();
        MerchantRole merchantRole = new MerchantRole();
        merchantRole.setRoleName("超级管理员");
        merchantRole.setLevel(0);
        merchantRole.setStatus(true);
        merchantRole.setRules("");
        merchantRole.setMerId(merId);
        merchantRole.setIsSuperAdmin(true);
        merchantRoleService.save(merchantRole);
        Integer roleId = merchantRole.getId();
        //添加商户超级管理员
        MerchantAdmin merchantAdmin = new MerchantAdmin();
        merchantAdmin.setAccount(request.getAccount());
        merchantAdmin.setRoles(roleId.toString());
        String password;
        if(StrUtil.isNotBlank(request.getPwd())){
            password = request.getPwd();
        }else {
            password =request.getMerPhone();
        }
        String pwd = HhUtil.encryptPassword(password, request.getAccount());
        merchantAdmin.setPwd(pwd);
        merchantAdmin.setMerId(merId);
        merchantAdmin.setIsSuperAdmin(true);
        merchantAdmin.setRealName(request.getMerFullName());
        merchantAdminService.save(merchantAdmin);

        //初始化商户菜单
        merchantMenuService.initMenus(merId);

        //初始化商户配置分类
        categoryService.initSettingCategory(merId);

        //初始化商户配置项
        //todo
        systemConfigService.initMerSettingS(merId);
        return Boolean.TRUE;
    }
}
